from django.db import models


class deployInfo(models.Model):
    '''
    应用列表
    '''

    name = models.CharField(max_length=30, null=False, blank=False, verbose_name="项目名", help_text="项目名称")
    port = models.IntegerField(null=False, blank=False, verbose_name="项目端口", help_text="项目端口")
    env = models.CharField(max_length=20, null=False, blank=False, verbose_name="环境", help_text="环境")
    application_type = models.CharField(max_length=8, null=False, blank=False, verbose_name='项目类型', help_text="项目类型")
    status = models.CharField(max_length=50, choices=(('unknown', '未知状态'), ('building', '正在构建'),
                                                      ('failed', '构建失败'), ('success', '构建成功')),
                              default='unknown', verbose_name="状态", help_text="状态")
    git_repo = models.CharField(max_length=200, null=False, blank=False, verbose_name="项目git", help_text="项目git")
    branch = models.CharField(max_length=50, null=False, blank=False, default="master", verbose_name="代码分支",
                              help_text="代码分支")
    domain = models.CharField(max_length=50, null=True, blank=True, verbose_name="主机记录", help_text="主机记录")
    params = models.TextField(null=True, blank=True, verbose_name="构建传参", help_text="构建传参")
    create_time = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, null=True, blank=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "应用列表"
        verbose_name_plural = verbose_name
        db_table = "deploy_info"

    def __str__(self):
        return self.name


class deploy_history(models.Model):
    '''
    构建历史
    '''
    name = models.CharField(max_length=30, null=True, blank=True, verbose_name="项目名", help_text="项目名")
    create_time = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name="生成时间", help_text="生成时间")
    logs = models.TextField(null=True, blank=True, verbose_name="日志内容", help_text="日志内容")
    status = models.BooleanField(verbose_name="构建状态", help_text="构建状态")
    application_type = models.CharField(max_length=8, null=False, blank=False, verbose_name='项目类型', help_text="项目类型")
    env = models.CharField(max_length=20, null=False, blank=False, verbose_name="环境", help_text="环境")
    # application = models.ForeignKey(deployInfo,on_delete=models.CASCADE,null=True,blank=True,verbose_name="应用信息",help_text="应用信息")
    message = models.CharField(max_length=252, null=True, blank=True, verbose_name="钉钉消息", help_text="钉钉消息")

    class Meta:
        verbose_name = "构建历史"
        verbose_name_plural = verbose_name
        db_table = "deploy_history"

    def __str__(self):
        return self.name


class deploy_sysconf_type(models.Model):
    '''项目类型'''
    name = models.CharField(max_length=50, unique=True, error_messages={"unique": "名称只能唯一"}, null=False, blank=False,
                            verbose_name="类型名称", help_text="类型名称")
    script_path = models.CharField(max_length=200, null=True, blank=True, verbose_name="脚本目录", help_text="脚本目录")
    desc = models.CharField(max_length=200, null=True, blank=True, verbose_name="描述", help_text="描述")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建日期", help_text="创建日期")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新日期", help_text="更新日期")

    class Meta:
        verbose_name = "项目类型管理"
        verbose_name_plural = verbose_name
        db_table = "deploy_conf_type"
        ordering = ['id']


class deploy_sysconf_envs(models.Model):
    '''环境管理'''
    name = models.CharField(max_length=50, unique=True, error_messages={"unique": "名称只能唯一"}, null=False, blank=False,
                            verbose_name="环境名称", help_text="环境名称")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建日期", help_text="创建日期")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新日期", help_text="更新日期")
    label = models.CharField(max_length=200, null=True, blank=True, verbose_name="描述", help_text="描述")

    class Meta:
        verbose_name = "环境管理"
        verbose_name_plural = verbose_name
        db_table = "deploy_conf_envs"
        ordering = ['id']


class work_order(models.Model):
    '''工单'''

    order_name = models.CharField(max_length=200, verbose_name="工单名称", help_text="工单名称")
    order_type = models.CharField(max_length=100, verbose_name="工单类型", help_text="工单类型")
    env = models.CharField(max_length=10, verbose_name="需要发布的环境", help_text="需要发布的环境")
    order_desc = models.TextField(verbose_name="工单说明", help_text="需要发布的应用")
    # push_status = models.BooleanField(verbose_name="镜像是否已推送",help_text="镜像是否已推送")
    submit_user = models.CharField(max_length=50, null=True, blank=True, verbose_name="用户",
                                   help_text="用户(此项不填写,系统自动生成)")
    audit_user = models.CharField(max_length=50, verbose_name="审核人", help_text="审核人")
    status = models.BooleanField(verbose_name="工单状态", null=True, blank=True, default=False, help_text="工单状态")
    plan_time = models.DateTimeField(auto_now_add=True, verbose_name="预计发布时间", help_text="预计发布时间")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="工单创建时间", help_text="工单创建时间")
    audit_time = models.DateTimeField(auto_now_add=True, verbose_name="工单结束时间", help_text="工单结束时间")

    class Meta:
        verbose_name = "工单"
        verbose_name_plural = verbose_name
        db_table = "work_order"
        ordering = ['id']

    def __str__(self):
        return self.order_name
